package com.example.mydemo.service.impl;

import com.example.mydemo.entity.LogBean;
import com.example.mydemo.service.LogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LogService {
    @Autowired
    private LogRepository logRepository;
    // 假设这是一个 Service 方法
    @Autowired
    private MongoTemplate mongoTemplate;
    public Map<String, Object> getLogsByPage(String key, String value,Integer pageNum, Integer pageSize) {
        Map<String, Object> mapResult = new HashMap<>(16);
            // 初始化分页参数（默认值）
            pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
            pageSize = (pageSize == null || pageSize < 1) ? 15 : pageSize;
            String collectionName = "logs";
            // 构建查询条件
            Criteria criteria = new Criteria();
            if(!key.isEmpty()){
                criteria.and(key).is(value);
            }

            // 恢复时间范围查询（若参数有效）
//            if (beginTime != null && endTime != null) {
//                criteria.and("createDate").gte(beginTime).lte(endTime);
//            }

            // 构建聚合查询（含排序）
            Aggregation aggregation = Aggregation.newAggregation(
                    Aggregation.match(criteria),
                    Aggregation.sort(Sort.by(Sort.Direction.DESC, "createDate")), // 应用排序
                    Aggregation.skip((pageNum - 1) * pageSize),
                    Aggregation.limit(pageSize)
            );

            // 执行聚合查询并获取结果（使用具体实体类 LogBean）
            List<LogBean> list = mongoTemplate.aggregate(aggregation, collectionName, LogBean.class)
                    .getMappedResults();

            // 统计总记录数
            Query query = new Query(criteria);
            long total = mongoTemplate.count(query, LogBean.class, collectionName);

            // 组装返回结果
            mapResult.put("data", list);
            mapResult.put("count", total);
            mapResult.put("pageNum", pageNum);
            mapResult.put("pageSize", pageSize);

            // 记录查询条件和排序规则（可选）
            Map<String, Object> where = new HashMap<>();
            where.put(key, value);
//            if (beginTime != null && endTime != null) {
//                where.put("createDate", "between=" + beginTime + "," + endTime);
//            }
            mapResult.put("where", where);

            Map<String, Object> order = new HashMap<>();
            order.put("createDate", "desc");
            order.put("userId", "asc");
            mapResult.put("order", order);
        return mapResult;
    }
}
